#include "graphic_linux_X11.h"

void drw_init(xcfg)
	struct X_CONFIG** xcfg;
{
	(*xcfg)->d = XOpenDisplay(NULL);
	if((*xcfg)->d == NULL)
	{
		fprintf(stderr, "Cannot open display.\n");
		exit(1);
	}

	(*xcfg)->s = DefaultScreen((*xcfg)->d);
	(*xcfg)->w = XCreateSimpleWindow(
		(*xcfg)->d, RootWindow((*xcfg)->d, (*xcfg)->s),
		10, 10, 100, 100, 1,
		BlackPixel((*xcfg)->d, (*xcfg)->s),
		WhitePixel((*xcfg)->d, (*xcfg)->s));

	XSelectInput((*xcfg)->d, (*xcfg)->w,
			ExposureMask | KeyPressMask);
	XMapWindow((*xcfg)->d, (*xcfg)->w);
}

void
drw_deinit(xcfg)
	struct X_CONFIG** xcfg;
{
	XCloseDisplay((*xcfg)->d);
}

void
drw_events(xcfg)
	struct X_CONFIG** xcfg;
{
	XNextEvent((*xcfg)->d, &((*xcfg)->e));
	if(((*xcfg)->e).type == Expose)
	{
		printf("EXPOSE\n");
		XStoreName((*xcfg)->d, (*xcfg)->w, "Hello, World!");
		XFillRectangle((*xcfg)->d, (*xcfg)->w,
				DefaultGC((*xcfg)->d, (*xcfg)->s),
				20, 20, 10, 10);

		return;
	}
}
